package com.example.myapplication

import android.app.ProgressDialog
import android.net.wifi.ScanResult
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.adapter.WifiInfoAdapter
import com.example.myapplication.databinding.ActivityMainBinding
import com.example.myapplication.utils.Timber
import com.example.myapplication.utils.WifiUtils
import com.example.myapplication.wifi.WifiAutoConnector
import com.example.myapplication.wifi.WifiScanHandler
import com.example.myapplication.wifi.WifiScanResultReceiver
import com.example.myapplication.wifi.WifiUtil
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions


class MainActivity : AppCompatActivity() ,ScanWifiResult{
    lateinit var binding: ActivityMainBinding
    lateinit var progressdlg: ProgressDialog
    private lateinit var wifiInfoAdapter: WifiInfoAdapter


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
        initData()
        initListener()
    }

    private fun initListener() {
        binding.scan.setOnClickListener {
            requestPermissions()
        }

        binding.connectWifi.setOnClickListener {
            val wifiUtils =WifiUtils(this)
            Timber.fi("------${wifiUtils.connectWifiTest(Constants.wifiName, Constants.wifiPwd)}")
//            WifiUtil.connectToWiFi(Constants.wifiName, Constants.wifiPwd)
        }
    }

    private fun initData() {
        progressDialog()
        WifiAutoConnector.init()
        WifiScanResultReceiver.setOnResult(this)
        binding.txtWifiName.text = WifiUtil.getWifiName()
    }

    private fun initView() {
        wifiInfoAdapter = WifiInfoAdapter()
        val mLinearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        binding.rvWifiList.layoutManager = mLinearLayoutManager
        binding.rvWifiList.adapter = wifiInfoAdapter
    }

    private fun requestPermissions() {
        XXPermissions.with(this)
            .permission(Permission.ACCESS_FINE_LOCATION)
            .permission(Permission.ACCESS_COARSE_LOCATION)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (!allGranted) {
                        Toast.makeText(this@MainActivity, "获取部分权限成功，但部分权限未正常授予", Toast.LENGTH_SHORT).show()
                        return
                    }
                    Toast.makeText(this@MainActivity, "获取权限成功!", Toast.LENGTH_SHORT).show()
                    WifiScanHandler.startScan()
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    if (doNotAskAgain) {
                        Toast.makeText(this@MainActivity, "被永久拒绝授权，请手动授予位置权限", Toast.LENGTH_SHORT)
                            .show()
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(this@MainActivity, permissions)
                    } else {
                        Toast.makeText(this@MainActivity, "获取位置权限失败", Toast.LENGTH_SHORT).show()
                    }
                }
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        WifiAutoConnector.unInit()
    }

    /**
     * init dialog
     */
    private fun progressDialog() {
        progressdlg = ProgressDialog(this)
        progressdlg.setCanceledOnTouchOutside(false)
        progressdlg.setCancelable(false)
        progressdlg.setProgressStyle(ProgressDialog.STYLE_SPINNER)
        progressdlg.setMessage(getString(R.string.wait_moment))
        progressdlg.show()
    }

    /**
     * dissmiss dialog
     */
    private fun progressDismiss() {
        if (progressdlg != null) {
            progressdlg.dismiss()
        }
    }

    override fun success(result: List<ScanResult>) {
        progressDismiss()
        val wifiInfos = WifiUtil.getScanWifiResult(result)
        wifiInfoAdapter.setList(wifiInfos)
    }

}